package com.approval.controller;

import com.approval.dto.Result;
import com.approval.dto.OrgTreeNode;
import com.approval.entity.Org;
import com.approval.service.OrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 组织架构控制器
 */
@RestController
@RequestMapping("/orgs")
@CrossOrigin(origins = "*")
public class OrgController {
    
    @Autowired
    private OrgService orgService;
    
    /**
     * 创建组织
     */
    @PostMapping
    public ResponseEntity<Result<Org>> createOrg(@Valid @RequestBody Org org) {
        try {
            Org createdOrg = orgService.createOrg(org);
            return ResponseEntity.ok(Result.success(createdOrg));
        } catch (Exception e) {
            return ResponseEntity.ok(Result.error(e.getMessage()));
        }
    }
    
    /**
     * 更新组织
     */
    @PutMapping("/{id}")
    public ResponseEntity<Result<Org>> updateOrg(@PathVariable Long id, @Valid @RequestBody Org org) {
        try {
            Org updatedOrg = orgService.updateOrg(id, org);
            return ResponseEntity.ok(Result.success(updatedOrg));
        } catch (Exception e) {
            return ResponseEntity.ok(Result.error(e.getMessage()));
        }
    }
    
    /**
     * 删除组织
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Result<Void>> deleteOrg(@PathVariable Long id) {
        try {
            orgService.deleteOrg(id);
            return ResponseEntity.ok(Result.success());
        } catch (Exception e) {
            return ResponseEntity.ok(Result.error(e.getMessage()));
        }
    }
    
    /**
     * 根据ID获取组织
     */
    @GetMapping("/{id}")
    public ResponseEntity<Result<Org>> getOrgById(@PathVariable Long id) {
        try {
            Org org = orgService.getOrgById(id);
            return ResponseEntity.ok(Result.success(org));
        } catch (Exception e) {
            return ResponseEntity.ok(Result.error(e.getMessage()));
        }
    }
    
    /**
     * 获取所有组织
     */
    @GetMapping("/all")
    public ResponseEntity<Result<List<Org>>> getAllOrgs() {
        try {
            List<Org> orgs = orgService.getAllOrgs();
            return ResponseEntity.ok(Result.success(orgs));
        } catch (Exception e) {
            return ResponseEntity.ok(Result.error(e.getMessage()));
        }
    }
    
    /**
     * 获取组织树
     */
    @GetMapping("/tree")
    public ResponseEntity<Result<List<OrgTreeNode>>> getOrgTree(@RequestParam(required = false) Long deptId, @RequestParam(required = false) String type) {
        try {
            List<OrgTreeNode> tree = orgService.getOrgTree(deptId, type);
            return ResponseEntity.ok(Result.success(tree));
        } catch (Exception e) {
            return ResponseEntity.ok(Result.error(e.getMessage()));
        }
    }
    
    /**
     * 根据父组织ID获取子组织列表
     */
    @GetMapping("/parent/{parentId}")
    public ResponseEntity<Result<List<Org>>> getOrgsByParentId(@PathVariable Long parentId) {
        try {
            List<Org> orgs = orgService.getOrgsByParentId(parentId);
            return ResponseEntity.ok(Result.success(orgs));
        } catch (Exception e) {
            return ResponseEntity.ok(Result.error(e.getMessage()));
        }
    }
    
    /**
     * 获取活跃组织列表
     */
    @GetMapping("/active")
    public ResponseEntity<Result<List<Org>>> getActiveOrgs() {
        try {
            List<Org> orgs = orgService.getActiveOrgs();
            return ResponseEntity.ok(Result.success(orgs));
        } catch (Exception e) {
            return ResponseEntity.ok(Result.error(e.getMessage()));
        }
    }
    
    /**
     * 根据用户名搜索用户
     */
    @GetMapping("/tree/user/search")
    public ResponseEntity<Result<List<OrgTreeNode>>> searchUsersByName(@RequestParam String userName) {
        try {
            List<OrgTreeNode> users = orgService.searchUsersByName(userName);
            return ResponseEntity.ok(Result.success(users));
        } catch (Exception e) {
            return ResponseEntity.ok(Result.error(e.getMessage()));
        }
    }
}